package com.tcm.doctor.service;

import cn.hutool.core.util.ObjectUtil;
import com.base.core.exception.ServiceException;
import com.base.core.utils.R;
import com.base.genid.generator.UidGenerator;
import com.base.core.pager.PageBean;
import com.base.core.pager.PageContext;
import com.base.core.dozer.DozerConvert;
import com.tcm.doctor.domain.dto.DoctorVerifyDTO;
import com.tcm.doctor.domain.dto.DoctorVerifyLogDTO;
import com.tcm.doctor.domain.entity.Doctor;
import com.tcm.doctor.domain.entity.DoctorJobtitle;
import com.tcm.doctor.domain.entity.DoctorVerify;
import com.tcm.doctor.domain.entity.DoctorVerifyLog;
import com.tcm.doctor.domain.enums.BusinessCodeEnum;
import com.tcm.doctor.mapper.DoctorJobtitleMapper;
import com.tcm.doctor.mapper.DoctorMapper;
import com.tcm.doctor.mapper.DoctorVerifyLogMapper;
import com.tcm.doctor.mapper.DoctorVerifyMapper;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@DubboService(version = "0.0.1-alpha", retries = 3, timeout = 6000, delay = -1)
public class DoctorVerifyServiceImpl implements DoctorVerifyService {

    @Autowired
    private DoctorVerifyMapper doctorVerifyMapper;

    @Autowired
    private DoctorVerifyLogMapper doctorVerifyLogMapper;

    @Autowired
    private DoctorMapper doctorMapper;

    @Autowired
    private DoctorJobtitleMapper doctorJobtitleMapper;


    @DubboReference(version = "0.0.1-beta",group = "cached-uid",timeout = 6000, check = false,retries = 3)
    private UidGenerator uidGenerator;

    /**
     * 获得医生认证及审核信息
     *
     * @param userId
     * @return Doctor
     */
    @Override
    public DoctorVerifyDTO findByUserId(Long userId) {
        DoctorVerify doctorVerify = doctorVerifyMapper.findByUserId(userId);
        if(ObjectUtil.isEmpty(doctorVerify)){
            return null;
        }

        DoctorVerifyDTO doctorVerifyDTO = new DoctorVerifyDTO();
        BeanUtils.copyProperties(doctorVerify, doctorVerifyDTO);
        Doctor doctor = doctorMapper.findByUserId(userId);
        if(ObjectUtil.isNotEmpty(doctor)){
            BeanUtils.copyProperties(doctor,doctorVerifyDTO);
        }
        //获得日志信息
        List<DoctorVerifyLog> doctorVerifyLogList = doctorVerifyLogMapper.listLogByVerifyId(doctorVerify.getId());
        List<DoctorVerifyLogDTO> doctorVerifyLogDTOList = DozerConvert.copyList(doctorVerifyLogList, DoctorVerifyLogDTO.class);
        doctorVerifyDTO.setDoctorVerifyLogDTOList(doctorVerifyLogDTOList);
        return doctorVerifyDTO;
    }

    /**
     * 新增医生认证信息
     *
     * @param doctorVerifyDto
     */
    @Transactional
    @Override
    public void save(DoctorVerifyDTO doctorVerifyDto) throws ServiceException {

        if(ObjectUtil.isEmpty(doctorVerifyDto.getUserId())){
            R.throwFail(BusinessCodeEnum.DOCTOR_USER_ID_NOT_EXISTS);
        }
        if(doctorVerifyDto.getUserId().equals(0L)){
            R.throwFail(BusinessCodeEnum.DOCTOR_USER_ID_NOT_EXISTS);
        }
        Doctor doctor = DozerConvert.copyProperties(doctorVerifyDto, Doctor.class);
        DoctorJobtitle doctorJobtitle = doctorJobtitleMapper.findById(doctor.getJobtitleId());
        if(ObjectUtil.isNotEmpty(doctorJobtitle)){
            doctor.setJobtitleLevel(doctorJobtitle.getLevel());
        }
        doctor.setUpdateTime(LocalDateTime.now());
        doctor.setDeleted(new Byte("0"));
        Doctor doc = doctorMapper.findByUserId(doctor.getUserId());
        if(ObjectUtil.isEmpty(doc)){
            doctor.setId(uidGenerator.getUID());
            doctor.setCreateTime(LocalDateTime.now());
            doctorMapper.insert(doctor);
        }else {
            doctor.setId(doc.getUserId());
            doctorMapper.update(doctor);
        }

        //认证用户信息判断
        DoctorVerify doctorVerify = doctorVerifyMapper.findByUserId(doctorVerifyDto.getUserId());
        if(ObjectUtil.isEmpty(doctorVerify)){
            doctorVerify = new DoctorVerify();
            BeanUtils.copyProperties(doctorVerifyDto, doctorVerify);
            doctorVerify.setCreateTime(LocalDateTime.now());
            doctorVerify.setStatus(new Byte("0"));
            doctorVerify.setDeleted(new Byte("0"));
            doctorVerify.setUpdateTime(LocalDateTime.now());
            doctorVerify.setId(uidGenerator.getUID());
            doctorVerifyMapper.insert(doctorVerify);
        }else {
            BeanUtils.copyProperties(doctorVerifyDto, doctorVerify);
            doctorVerify.setUpdateTime(LocalDateTime.now());
            doctorVerifyMapper.update(doctorVerify);
        }

    }

    @Transactional
    @Override
    public void verifyPass(DoctorVerifyLogDTO doctorVerifyLogDTO){
        if(ObjectUtil.isEmpty(doctorVerifyLogDTO.getUserId())){
            R.throwFail(BusinessCodeEnum.DOCTOR_USER_ID_NOT_EXISTS);
        }
        if(doctorVerifyLogDTO.getUserId().equals(0L)){
            R.throwFail(BusinessCodeEnum.DOCTOR_USER_ID_NOT_EXISTS);
        }
        Doctor doctor = doctorMapper.findByUserId(doctorVerifyLogDTO.getUserId());
        if(ObjectUtil.isEmpty(doctor)){
            R.throwFail(BusinessCodeEnum.DOCTOR_USER_NOT_EXISTS);
        }
        doctor.setUpdateTime(LocalDateTime.now());
        doctor.setUserId(doctorVerifyLogDTO.getUserId());
        doctor.setIsVerify(new Byte("1"));
        doctorMapper.update(doctor);
        DoctorVerify doctorVerify = doctorVerifyMapper.findByUserId(doctorVerifyLogDTO.getUserId());
        if(ObjectUtil.isEmpty(doctorVerify)){
            R.throwFail(BusinessCodeEnum.DOCTOR_VERIFY_CHECK_FAIL);
        }
        doctorVerify.setUpdateTime(LocalDateTime.now());
        doctorVerify.setStatus(new Byte("1"));
        doctorVerify.setRemark("审核通过");
        doctorVerify.setUserId(doctorVerifyLogDTO.getUserId());
        doctorVerifyMapper.update(doctorVerify);
        DoctorVerifyLog doctorVerifyLog = new DoctorVerifyLog();
        BeanUtils.copyProperties(doctorVerifyLogDTO, doctorVerifyLog);
        doctorVerifyLog.setContent("审核通过");
        doctorVerifyLog.setId(uidGenerator.getUID());
        doctorVerifyLog.setCreateTime(LocalDateTime.now());
        doctorVerifyLog.setUpdateTime(LocalDateTime.now());
        doctorVerifyLogMapper.insert(doctorVerifyLog);
    }

    @Transactional
    @Override
    public void verifyRefused(DoctorVerifyLogDTO doctorVerifyLogDTO){
        if(ObjectUtil.isEmpty(doctorVerifyLogDTO.getUserId())){
            R.throwFail(BusinessCodeEnum.DOCTOR_USER_ID_NOT_EXISTS);
        }
        if(doctorVerifyLogDTO.getUserId().equals(0L)){
            R.throwFail(BusinessCodeEnum.DOCTOR_USER_ID_NOT_EXISTS);
        }
        DoctorVerify doctorVerify = doctorVerifyMapper.findByUserId(doctorVerifyLogDTO.getUserId());
        if(ObjectUtil.isEmpty(doctorVerify)){
            R.throwFail(BusinessCodeEnum.DOCTOR_VERIFY_CHECK_FAIL);
        }
        doctorVerify.setUpdateTime(LocalDateTime.now());
        doctorVerify.setStatus(new Byte("-1"));
        doctorVerify.setRemark(doctorVerifyLogDTO.getContent());
        doctorVerify.setUserId(doctorVerifyLogDTO.getUserId());
        doctorVerifyMapper.update(doctorVerify);
        DoctorVerifyLog doctorVerifyLog = new DoctorVerifyLog();
        BeanUtils.copyProperties(doctorVerifyLogDTO, doctorVerifyLog);
        doctorVerifyLog.setId(uidGenerator.getUID());
        doctorVerifyLog.setCreateTime(LocalDateTime.now());
        doctorVerifyLog.setUpdateTime(LocalDateTime.now());
        doctorVerifyLogMapper.insert(doctorVerifyLog);
    }

    /**
     * 永久删除
     *
     * @param userId
     */
    @Override
    public void delete(Long userId) {
        doctorVerifyMapper.deleteByUserId(userId);
        doctorVerifyLogMapper.deleteByUserId(userId);
    }

    /**
     * 删除到回收站
     *
     * @param userId
     */
    @Override
    public void deleteLogic(Long userId) {
        doctorVerifyMapper.deleteLogicByUserId(userId);
        doctorVerifyLogMapper.deleteLogicByUserId(userId);
    }

    /**
     * 根据状态分页查询认证信息列表
     * @param status
     * @param pagesize
     * @return
     */
    @Override
    public PageBean<DoctorVerifyDTO> listVerifyPageByStatus(Byte status, Long start, Long pagesize){
        int total = doctorVerifyMapper.countVerifyByStatus(status);
        PageContext.setPageSize(pagesize);
        PageContext.setCurrentPage(start);
        PageContext.setDataCount(new Long(total));
        if(total == 0){
            return new PageBean<>(null);
        }
        List<DoctorVerifyDTO> doctorVerifyDTOList = doctorVerifyMapper.listVerifyPageByStatus(status,PageContext.getStart(),pagesize);
        PageBean<DoctorVerifyDTO> doctorVerifyVoPageBean = new PageBean<>(doctorVerifyDTOList);
        return doctorVerifyVoPageBean;
    }

    /**
     * 分页查询认证信息列表
     * @param start
     * @param pagesize
     * @return
     */
    @Override
    public PageBean<DoctorVerifyDTO> listVerifyPage(Long start, Long pagesize){
        int total = doctorVerifyMapper.countVerify();
        PageContext.setPageSize(pagesize);
        PageContext.setCurrentPage(start);
        PageContext.setDataCount(new Long(total));
        if(total == 0){
            return new PageBean<>(null);
        }

        List<DoctorVerifyDTO> doctorVerifyDTOList = doctorVerifyMapper.listVerifyPage(PageContext.getStart(),pagesize);
        PageBean<DoctorVerifyDTO> doctorVerifyVoPageBean = new PageBean<>(doctorVerifyDTOList);
        return doctorVerifyVoPageBean;
    }


}
